#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 88;
LL dp[MAXN][MAXN][MAXN*MAXN];

int main(int argc, char** argv) {
	#ifdef hai
	freopen("in.txt", "r", stdin);
	#endif
	int T;
	cin>>T;
	int n, m;
	LL k;
	while (T--) {
		scanf("%d %d %lld", &n, &m, &k);
//		cout<<n<<" "<<m<<" "<<k<<endl;
		memset(dp, 0, sizeof(dp));
		dp[1][1][1] = n * m % k;
		for (int x=1; x<=n; ++x) {
			for (int y=1; y<=m; ++y) {
				int len = x*y;
				for (int i=1; i<=len; ++i) {
					LL& d = dp[x][y][i];
					if (!d) continue;
					if (x+1<=n) {
						LL& t = dp[x+1][y][i+1];
						t = (t + y*(n-x)*d) % k;
					}
					if (y+1<=m) {
						LL& t = dp[x][y+1][i+1];
						t = (t + x*(m-y)*d) % k;
					}
					if (x*y>=i) {
						LL& t = dp[x][y][i+1];
						t = (t + (x*y-i)*d) % k;
					}
				}
			}
		}
		cout<<dp[n][m][n*m]<<endl;
	}
	return 0;
} 
